package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("AdditiveController")
public class AdditiveController extends CmiEmaController {

	@RequestMapping("/listAdditives")
	public ModelAndView listAdditives(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = additivePaginationService.updatePagination(request, pagination);
		Set<Additive> additives = additivePaginationService.getPaginationAdditives(pagination);

		mav.addObject("additives", additives);
		mav.addObject("entityName", "additive");
		mav.addObject("pagination", pagination);
		mav.setViewName("additive/listAdditives.jsp");
		return mav;
	}

	public String listAdditives() {
		return "redirect:/listAdditives";
	}

	@RequestMapping("/viewAdditive")
	public ModelAndView viewAdditive(@RequestParam Integer additiveId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("additive", additiveDAO.findAdditiveByPrimaryKey(additiveId));
		mav.setViewName("additive/viewAdditive.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteAdditives")
	public ModelAndView confirmDeleteAdditives(
			@RequestParam(value = "deleteAdditivesIds", required = true) Set<Integer> deleteAdditivesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Additive> deleteAdditives = additiveDAO.findAdditivesByIds(deleteAdditivesIds);
		Set<Additive> referencedAdditives = additiveService.loadReferenceAdditives(deleteAdditives);
		boolean hasReferenced;
		if (referencedAdditives.size() == 0) {
			mav.addObject("deleteEntitys", deleteAdditives);
			hasReferenced = false;
		} else {
			deleteAdditives.removeAll(referencedAdditives);
			mav.addObject("referencedEntitys", referencedAdditives);
			mav.addObject("deleteEntitys", deleteAdditives);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Additive");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listAdditives");
		return mav;
	}

	@RequestMapping("/deleteAdditive")
	public String deleteAdditive(@RequestParam Integer additiveId) {
		Additive additive = additiveDAO.findAdditiveByPrimaryKey(additiveId);
		additiveService.deleteAdditive(additive);
		return "forward:/listAdditives";
	}

	@RequestMapping("/confirmDeleteAdditive")
	public ModelAndView confirmDeleteAdditive(@RequestParam Integer additiveId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("additive", additiveDAO.findAdditiveByPrimaryKey(additiveId));

		mav.setViewName("additive/deleteAdditive.jsp");

		return mav;
	}

	@RequestMapping("/deleteAdditives")
	public String deleteAdditives(
			@RequestParam(value = "deleteAdditivesId", required = true) Set<Integer> deleteAdditivesId) {
		additiveService.deleteAdditives(deleteAdditivesId);
		return "forward:/listAdditives";
	}

	@RequestMapping("/editAdditive")
	public ModelAndView editAdditive(@RequestParam Integer additiveId) {

		ModelAndView mav = new ModelAndView();

		Additive additive = additiveDAO.findAdditiveByPrimaryKey(additiveId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("additive", additive);

		// 把所有的引用additive对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(additiveService.loadAdditiveObjectReferencedSet(additive));

		// 将编辑页面additive添加到mav中
		mav.setViewName("additive/editAdditive.jsp");
		return mav;
	}

	@RequestMapping("/newAdditive")
	public ModelAndView newAdditive() {
		ModelAndView mav = new ModelAndView();
		Additive additive = new Additive();

		mav.addObject("additive", additive);

		// 把所有的引用additive对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(additiveService.loadAdditiveObjectReferencedSet(additive));

		mav.addObject("newFlag", true);
		mav.setViewName("additive/editAdditive.jsp");

		return mav;
	}

	@RequestMapping("/saveAdditive")
	public ModelAndView saveAdditive(@ModelAttribute Additive additive) {

		ModelAndView mav = new ModelAndView();

		Additive existingAdditive = CmiHibernateProxyUtil
				.deproxy(additiveDAO.findAdditiveByPhysicalPrimaryKey(additive.getName()));

		// 新建记录
		if (additive.getId() == null) {
			if (additive.equals(existingAdditive)) {

				mav.addAllObjects(additiveService.loadAdditiveClassReferencedSet(additive));
				// mav = editContinue(mav, additive);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("additive/editAdditive.jsp");
			} else {
				additiveService.saveAdditive(additive);
				mav.setViewName("forward:/listAdditives");
			}
		}
		// 编辑记录
		else {
			Additive originAdditive = CmiHibernateProxyUtil
					.deproxy(additiveDAO.findAdditiveByPrimaryKey(additive.getId()));
			if (!additive.equals(originAdditive) && additive.equals(existingAdditive)) {
				mav.addAllObjects(additiveService.loadAdditiveClassReferencedSet(additive));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("additive/editAdditive.jsp");
			} else {
				additiveService.saveAdditive(additive);
				mav.setViewName("forward:/listAdditives");
			}
		}
		return mav;
	}

	@RequestMapping("/additiveController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
